package com.aaa.controller;

import com.aaa.entity.AlertCode;
import com.aaa.entity.Dept;
import com.aaa.entity.Userinfo;
import com.aaa.service.DeptService;
import com.aaa.util.MyUtil;
import jdk.nashorn.internal.ir.IdentNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.AbstractList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zhouhangyong
 * @date ：Created in 2022/6/23 8:44
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("dept")
public class DeptController {
    @Autowired
    DeptService deptService;

    @GetMapping("deptList.do")
    public AlertCode deptList(HttpServletRequest request){
        Map map = MyUtil.parameterNamesUtil(request, null);
        System.out.println(map);
        AlertCode alertCode = deptService.listDept(map);
        return alertCode;
    }
    @DeleteMapping("/{id}/del.do")
    public AlertCode delList(@PathVariable("id") String id){
        AlertCode alertCode = deptService.delete(id);
        return alertCode;
    }
    @PostMapping("addList.do")
    public AlertCode addList(Dept dept){
        AlertCode alertCode = deptService.add(dept);
        return alertCode;
    }
    @PutMapping("updateDept.do")
    public AlertCode updateUserinfo(Dept dept){
        System.out.println(dept.toString());
        AlertCode updateDept = deptService.updateDept(dept);
        return updateDept;
    }
    @GetMapping("updateKey.do")
    public AlertCode updateKey(Integer id){
        AlertCode updateKey = deptService.updateKey(id);
        return updateKey;
    }

    // 唯一性校验
//    @RequestMapping("uniqueInfo.do")
    @GetMapping("uniqueInfo.do")
    public AlertCode uniqueInfo(String deptname, Integer deptid){
        System.out.println(deptname+"==="+deptid);
        List<Map> list = deptService.queryDeptname(deptname, deptid);
        if(list!=null && list.size()>0){
            return new AlertCode(-1,"已经存在不可使用");
        } else {
            return new AlertCode(1,"可使用正常使用");
        }
    }
}
